package com.syh.jn.scm.domain.operation.entity.financial;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.dto.operation.financial.FinancialContactDto;
import com.syh.jn.scm.dto.operation.financial.FinancialContactMto;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 运营端关联供应商
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class FinancialContact extends AbstractSimpleAggregate<Long> {
  /**
   * 供应商Id
   */
  private Long supplierId;
  /**
   * 贷款方式(0:应收保理,1:采购垫资 |0|1|,可单选,多选)
   */
  private String loanMode;
  /**
   * 合作状态(1：合作中  -1：暂停合作)
   */
  private Short cooperatingState;
  /**
   * 总贷款额度
   */
  private BigDecimal totalModeAmount;
  /**
   * 合同附件图片url,保存格式json字符串{"contractUrl":"www.test.com","contractUrl":"www.demo.com"}
   */
  private String contractUrl;

  public FinancialContact() {
  }

  /**
   * 关联供应商
   *
   * @param dto
   */
  public FinancialContact(FinancialContactDto dto) {
    ExtAssert.notNull(dto,"关联供应商对象不能为空");
    ValidationUtils.validate(dto);
    this.supplierId = dto.getSupplierId();
    this.initDefault();
    //合作中
    this.inCooperating();
    this.loanMode = dto.getLoanMode();
    this.totalModeAmount = dto.getTotalModeAmount();
    this.contractUrl = dto.getContractUrl().toString();
  }

  /**
   * 合作中
   */
  public void inCooperating(){
    this.cooperatingState = 1;
  }

  /**
   * 暂停合作
   */
  public void suspendCooperation(){
    this.cooperatingState = -1;
  }

  /**
   * 修改
   *
   * @param mto
   */
  public void modify(FinancialContactMto mto) {
    ExtAssert.notNull(mto,"对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto,this);
  }

  /**
   * 删除
   */
  public void destroy() {
    if(this.cooperatingState.equals(Short.valueOf("1"))){
        throw new BusinessException("合作中不能删除");
    }
  }
}
